<?php
namespace app\appadmin\controller\v1;
use app\app\Appbase;

class Member extends Appbase 
{	
	
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct(){
    	if(request()->identity == 3){
      		return $this->ReError( '您没有当前操作权限！' );
    	}
		parent::__construct();
    }
	
	
	/**
	* 数据中心
	* /appadmin/v1/member/index
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function index($shop_id,$type=1){
		$shop_id = $shop_id == -1 ? request()->shopId  : $shop_id;
		try {
			$t = 'today';
			if( $type == 2 ) $t = 'month';
			if( $type == 3 ) $t = 'year';
			
			// TODO 充值
			$recharge = model('UserRecharge')->alias('a')
				->leftJoin('user b','b.id = a.uid')	
				->field(" ifnull(sum(a.price),0) as  prices ")
				->field(" ifnull(sum(a.give_balance),0) as gift_balance")
				->field(" ifnull(sum(a.give_integral),0) as gift_integral")
				->field(" ifnull(count(b.id),0)  as user_num")
				->where('a.status',1)
				->whereTime('a.status_time',$t)
				->where('b.shop_id',$shop_id)
				->findOrEmpty();
					
			// TODO 消费
			$xf['balance'] = model('user_bill')->alias('a')
				->leftJoin('user b','b.id = a.uid')
				->whereTime('a.add_time',$t)
				->where('b.shop_id',$shop_id)
				->where('a.category','balance')
				->where('a.pm',0)
				->where('a.status',1)
				->sum('a.number');
			$xf['gift_balance'] = model('user_bill')->alias('a')
				->leftJoin('user b','b.id = a.uid')
				->whereTime('a.add_time',$t)
				->where('b.shop_id',$shop_id)
				->where('a.category','balance_gift')
				->where('a.pm',0)
				->where('a.status',1)
				->sum('a.number');
			
			// TODO 当前余额
			$total = model('user')->alias('a')
				->field("ifnull(sum(a.balance),0) as  balance")
				->field("ifnull(sum(a.balance_gift),0) as gift_balance")
				->field("ifnull(sum(a.integral),0) as integral")
				->where('a.shop_id',$shop_id)
				->findOrEmpty();
			
			// TODO 订单
			$order['wx']['sw'] = model('order')->alias('a')
				->where('a.shop_id',$shop_id)
				->where('a.type','not in','1,2')
				->where('a.pay_type','<>','balance')
				->whereTime('a.add_time',$t)
				->where('a.is_pay',1)
				->where('a.status','>=',0)
				->sum('pay_price');
			$order['wx']['fw'] = model('order')->alias('a')
				->where('a.shop_id',$shop_id)
				->where('a.type','in','1,2')
				->where('a.pay_type','<>','balance')
				->whereTime('a.add_time',$t)
				->where('a.is_pay',1)
				->where('a.status','>=',0)
				->sum('pay_price');
			$order['ye']['sw'] = model('order')->alias('a')
				->where('a.shop_id',$shop_id)
				->where('a.type','not in','1,2')
				->where('a.pay_type','balance')
				->whereTime('a.add_time',$t)
				->where('a.is_pay',1)
				->where('a.status','>=',0)
				->sum('pay_price');
			$order['ye']['fw'] = model('order')->alias('a')
				->where('a.shop_id',$shop_id)
				->where('a.type','in','1,2')
				->where('a.pay_type','balance')
				->whereTime('a.add_time',$t)
				->where('a.is_pay',1)
				->where('a.status','>=',0)
				->sum('pay_price');
				
			return $this->ReSucess(compact('recharge','xf','total','order') );	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
		
	}
	
	
	
}



